package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IProductDao;
import com.cn.wanxi.dao.impl.ProductDaoImpl;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import redis.clients.jedis.Jedis;

import java.util.List;

public class ProductServiceImpl implements IProductService {
    IProductDao productDao = new ProductDaoImpl();
    @Override
    public List<ProductModel> getProductList() {
        Jedis jedis = new Jedis();
        if (jedis.exists("product_id")){
            return productDao.getProductListByRedis();
        }else {
            List<ProductModel> list = productDao.getProductList();
            productDao.setProductListInRedis(list);
            return list;
        }
    }

    @Override
    public ProductModel getProduct(int id) {
        return productDao.getProduct(id);
    }

    @Override
    public int addProduct(ProductModel productModel) {
        return productDao.addProduct(productModel);
    }

    @Override
    public int deleteProduct(int id) {
        return productDao.deleteProduct(id);
    }

    @Override
    public int updateProduct(ProductModel productModel) {
        return productDao.updateProduct(productModel);
    }

    @Override
    public List<ProductModel> searchProduct(String sql) {
        return productDao.searchProduct(sql);
    }

    @Override
    public List<ProductModel> getProductList(int page, int size) {
        return productDao.getProductList(page, size);
    }

    @Override
    public List<ProductModel> getProductList(int page, int size, String sort) {
        return productDao.getProductList(page, size, sort);
    }

    @Override
    public long getProductCount() {
        return productDao.getProductCount();
    }

    @Override
    public int updateEnable(int id, int enable) {
        return productDao.updateEnable(id,enable);
    }
}
